package com.tuanzi.loan.business.service.statistics.impl;

import com.tuanzi.loan.business.entity.statistics.ImeiMatchRecord;
import com.tuanzi.loan.business.entity.statistics.ImeiRecord;
import com.tuanzi.loan.business.entity.statistics.ImeiStat;
import com.tuanzi.loan.business.repository.statistics.ImeiStatRepository;
import com.tuanzi.loan.business.service.statistics.ImeiStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.imei.ImeiStatRequest;
import com.tuanzi.loan.vo.response.statistics.imei.ImeiStatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class ImeiStatServiceImpl implements ImeiStatService {

    @Autowired
    private ImeiStatRepository repository;

    @Override
    public Page<ImeiStatResponse> list(PageSearcher<ImeiStatRequest> pageSearcher) {
        return repository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public void importRecord(ImeiRecord record) {
        Integer registerCount = repository.getRegisterCount(record);
        Integer loginCount = repository.getLoginCount(record);
        repository.insert(new ImeiStat(record.getCreateDate(), record.getPrdId(), record.getChannelCode(), registerCount, loginCount));
    }

    @Override
    public void deleteById(Long id) {
        repository.deleteById(id);
    }

    @Override
    public ImeiMatchRecord exportRecord(ImeiRecord record) {
        return new ImeiMatchRecord(repository.getLoginList((record)), repository.getRegisterList(record));
    }
}
